// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VisualizerAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Manipulation.Visualization
{
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Media3D;
    using Microsoft.Robotics.Manipulation.Runtime;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// A class for environment display agent
    /// </summary>
    [DataContract]
    public class VisualizerAgent : ModelAgent<TimerMessage, VisualizerModel>,
        IControlReceiver<UpdateRobotMessage>,
        IControlReceiver<UpdateStaticEnvironmentMessage>,
        IControlReceiver<UpdateFrameTrajectoryMessage>,
        IControlReceiver<UpdatePointCloudMessage>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VisualizerAgent" /> class.
        /// </summary>
        /// <param name="name">Name of this agent</param>
        /// <param name="producer">The producer</param>
        /// <param name="maxUpdateIntervalMilliseconds">Max interval for update</param>
        public VisualizerAgent(
            string name,
            IProducer<TimerMessage> producer,
            int maxUpdateIntervalMilliseconds)
            : base(name, producer.Name, maxUpdateIntervalMilliseconds)
        {
        }

        /// <summary>
        /// Initialize the agent
        /// </summary>
        /// <param name="locator">Agent locator</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
        }

        /// <summary>
        /// Update robot model in the visualizer
        /// </summary>
        /// <param name="message">An update robot message</param>
        public void ReceiveControl(UpdateRobotMessage message)
        {
            this.Model.RobotModelView3DBindingSource = message.LinkGroup;
        }

        /// <summary>
        /// Update frame trajectory
        /// </summary>
        /// <param name="message">An update frame trajectory message</param>
        public void ReceiveControl(UpdateFrameTrajectoryMessage message)
        {
            this.Model.FrameTrajectoryBindingSource = message.FrameTrajectory;
        }

        /// <summary>
        /// Update environment
        /// </summary>
        /// <param name="message">An update environment message</param>
        public void ReceiveControl(UpdateStaticEnvironmentMessage message)
        {
            this.Model.StaticBackgroundModelView3DBindingSource = message.ObjectGroup;
        }

        /// <summary>
        /// Update point cloud
        /// </summary>
        /// <param name="message">An update point cloud message</param>
        public void ReceiveControl(UpdatePointCloudMessage message)
        {
            Point3DCollection pc = new Point3DCollection();

            for (int i = 0; i < message.PointCloud.Count; ++i)
            {
                pc.Add(message.PointCloud[i]);
            }

            pc.Freeze();
            this.Model.PointCloudBindingSource = pc;
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="message">The new state from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(TimerMessage message)
        {
            return true;
        }
    }
}
